package com.corpgovernment.organization.dataloader.db;

import com.corpgovernment.organization.entity.db.MbUserOrgRelation;
import com.corpgovernment.organization.mapper.MbUserOrgRelationMapper;
import com.corpgovernment.organization.model.MbUserOrgRelationModel;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zl.cui
 * @date 2023/10/12
 */
@Component
public class MbUserOrgRelationLoader {
    @Autowired
    private MbUserOrgRelationMapper mbUserOrgRelationMapper;

    public List<MbUserOrgRelationModel> getRelationByOrgIdList(List<String> orgIdList){
        if (CollectionUtils.isEmpty(orgIdList)){
            return Collections.emptyList();
        }
        List<MbUserOrgRelation> relationList = mbUserOrgRelationMapper.listByOrgIds(orgIdList);
        if (CollectionUtils.isEmpty(relationList)){
            return Collections.emptyList();
        }
        return relationList.stream().map(this::convert).collect(Collectors.toList());
    }

    public MbUserOrgRelationModel getRelationByUid(String uid){
        if (StringUtils.isBlank(uid)){
            return null;
        }
        List<MbUserOrgRelation> relationList = mbUserOrgRelationMapper.listByUid(uid);
        if (CollectionUtils.isEmpty(relationList)){
            return null;
        }
        return convert(relationList.get(0));
    }

    public List<MbUserOrgRelationModel> getRelationByUidList(List<String> uidList){
        if (CollectionUtils.isEmpty(uidList)){
            return Collections.emptyList();
        }
        Example example = new Example(MbUserOrgRelation.class);
        example.createCriteria()
                .andIn("uid", uidList)
                .andEqualTo("deleteTime", new Date(0));
        List<MbUserOrgRelation> relationList = mbUserOrgRelationMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(relationList)){
            return Collections.emptyList();
        }
        return relationList.stream().map(this::convert).collect(Collectors.toList());
    }

    private MbUserOrgRelationModel convert(MbUserOrgRelation relation){
        MbUserOrgRelationModel model = new MbUserOrgRelationModel();
        model.setUid(relation.getUid());
        model.setStatus(relation.getStatus());
        model.setOrgId(relation.getOrgId());
        model.setPostId(relation.getPostId());
        model.setVipLevel(relation.getVipLevel());
        model.setIsLeader(relation.getIsLeader());
        return model;
    }

    public Map<String, Map<String, Long>> countUidByOrgIds(List<String> orgIds){
        if (CollectionUtils.isEmpty(orgIds)){
            return CollectionUtils.newHashMap();
        }
        return mbUserOrgRelationMapper.countUidByOrgIds(orgIds);
    }

    public List<MbUserOrgRelationModel> listByCondition(List<String> orgIds, List<Long> postIdList, Integer status){
        Example example = new Example(MbUserOrgRelation.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleteTime", new Date(0));
        if (CollectionUtils.isNotEmpty(orgIds)){
            criteria.andIn("orgId", orgIds);
        }
        if (CollectionUtils.isNotEmpty(postIdList)){
            criteria.andIn("postId", postIdList);
        }
        if (Objects.nonNull(status)){
            criteria.andEqualTo("status", status);
        }
        List<MbUserOrgRelation> relationList = mbUserOrgRelationMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(relationList)){
            return Collections.emptyList();
        }
        return relationList.stream().map(this::convert).collect(Collectors.toList());
    }

    public List<MbUserOrgRelationModel> getRelationByOrgIdListNew(List<String> orgIdList){
        if (CollectionUtils.isEmpty(orgIdList)){
            return Collections.emptyList();
        }
        List<MbUserOrgRelation> relationList = mbUserOrgRelationMapper.listByOrgIdsNew(orgIdList);
        if (CollectionUtils.isEmpty(relationList)){
            return Collections.emptyList();
        }
        return relationList.stream().map(this::convert).collect(Collectors.toList());
    }
}
